<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let arr = [, 2, , 4, 5]
    let res = arr.reduce(function (sum, item) {
      return sum + item
    })
    console.log(res);
    Array.prototype.reduce2 = function (callback, initialValue) {
      if (this == null) {
        throw new TypeError('this is null or not defined')
      }
      if (typeof callback !== "function") {
        throw new TypeError(callback + ' is not a function')
      }
      let acc = 0
      let k = 0
      if (initialValue) {
        acc = initialValue
      } else {
        while (k < this.length) {
          if (this[k]) {
            acc = this[k++]
            break
          }
          k++
        }
      }
      while (k < this.length) {
        if (this[k]) {
          acc = callback(acc, this[k], k, this)
        }
        k++
      }
      return acc
    }
    res = arr.reduce2(function (sum, item) {
      return sum + item
    })
    console.log(res);

  </script>
</body>

</html>